<template>
  <div>
    <div style="margin: 10px 0">
      <el-input
        v-model="orderId"
        style="width: 200px"
        placeholder="请输入顾客名字"
        suffix-icon="el-icon-position"
        class="mll-5"
      />
      <el-button class="mll-5" type="primary" @click="fetchDataFromApi">搜索</el-button>
      <el-button type="warning" @click="reset">重置</el-button>
      <el-button type="primary" @click="handleAdd">新增<i class="el-icon-circle-plus-outline" /></el-button>
    </div>
    <el-card class="box-card">
      <el-table :data="menueorderData" style="width: 100%">
        <el-table-column label="订单编号" prop="orderId" />
        <el-table-column label="菜品编号" prop="menuId" />
        <el-table-column label="菜品数量" prop="menuNumber" />
        <el-table-column label="菜品名称" prop="menuName" />
        <el-table-column label="菜品价格" prop="menuPrice" />
        <el-table-column label="餐桌号" prop="tableId" />
        <el-table-column label="下单时间" prop="menuorderTimestart" />
        <el-table-column align="right">
          <template slot="header" slot-scope="scope">
            <el-input v-model="search" size="mini" placeholder="输入关键字搜索" />
          </template>
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button size="mini" type="danger" @click="handleDelete(scope.row.orderId,scope.row.menuId)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <span class="demonstration" />
        <div style="padding: 10px 0; text-align: center;">
          <el-pagination
            :current-page="pageNum"
            :page-sizes="[5, 8, 10, 20]"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </div>
      <el-dialog title="菜谱订单信息" :visible.sync="dialogFormVisible" width="50%">
        <el-form label-width="80px" size="small">
          <el-form-item label="订单编号">
            <el-input v-model="form.orderId" autocomplete="off" />
          </el-form-item>
          <el-form-item label="菜谱编号">
            <el-input v-model="form.menuId" autocomplete="off" />
          </el-form-item>
          <el-form-item label="菜品数量">
            <el-input v-model="form.menuNumber" autocomplete="off" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="handleSave">{{ isEditMode ? '修改' : '确定' }}</el-button>
        </div>
      </el-dialog>
    </el-card>
  </div>
</template>
<!-- orderId menuId menuNumber -->
<!-- orderId menuId menuNumber customerSex -->
<script>
import axios from 'axios'
import service from '@/utils/request'
export default {
  data() {
    return {
      menueorderData: [],
      search: '',
      pageNum: 1,
      pageSize: 5,
      total: 0,
      orderId: '',
      form: {},
      dialogFormVisible: false,
      fileList: [],
      isEditMode: false
    }
  },
  created() {
    this.fetchDataFromApi()
  },
  methods: {
    handleSizeChange(pageSize) {
      console.log(pageSize)
      this.pageSize = pageSize
      this.fetchDataFromApi()
    },
    handleCurrentChange(pageNum) {
      console.log(pageNum)
      this.pageNum = pageNum
      this.fetchDataFromApi()
    },
    fetchDataFromApi() {
      service.get('http://localhost:9090/menu_order?page=' + this.pageNum + '&pageSize=' + this.pageSize + '&orderid=' + this.orderId)
        .then(res=> {
          this.total = res.data.total
          this.menueorderData = res.data.rows
        })
        .catch(error => {
          console.error('Error fetching data from API:', error)
        })
    },
    reset() {
      this.orderId = ''
      this.fetchDataFromApi()
    },
    handleSave() {
      if (this.isEditMode) {
        this.updata()
      } else {
        this.save()
      }
    },
    handleAdd() {
      this.dialogFormVisible = true
      this.form = {}
    },
    save() {
      service.post('http://localhost:9090/menu_order/add', this.form).then(res => {
        if (res) {
          this.$message.success('保存成功')
          this.dialogFormVisible = false
          this.fetchDataFromApi()
        } else {
          this.$message.error('保存失败')
        }
      })
    },
    delete(orderId, menuId) {
      service.delete('http://localhost:9090/menu_order/delete?order_id=' + orderId + '&menu_id=' + menuId).then(res => {
        if (res) {
          this.$message.success('删除成功')
          this.delVisibleaa = false
          this.fetchDataFromApi()
        } else {
          this.$message.error('删除失败')
        }
      })
    },
    handleEdit(row) {
      this.form = row
      this.dialogFormVisible = true
      this.isEditMode = true
    },
    handleDelete(orderId, menuId) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: true
      }).then(() => {
        this.delete(orderId, menuId)
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    updata() {
      service.put('http://localhost:9090/menu_order/updata', this.form).then(res => {
        if (res) {
          this.$message.success('保存成功')
          this.dialogFormVisible = false
          this.fetchDataFromApi()
        } else {
          this.$message.error('保存失败')
        }
      })
    }
  }
}
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}

.mll-5 {
  margin-left: 10px;
}
</style>
